﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DrugsShopManagement.DBCLASS;

namespace DrugsShopManagement
{
    public partial class FrmDrugsIn : DevExpress.XtraEditors.XtraForm
    {
        public FrmDrugsIn(string user_no,string user_name)
        {
            InitializeComponent();
            u_no = user_no;
            u_name = user_name;
        }
        Operator op = new Operator();
        public string u_no, u_name, drugsCount="0",drugsCountAfterDrugsIn;//用户工号，用户姓名，药品当前库存量:默认为0,药品入库后库存量
        private FrmDrugsInDetail frmDrugsInDetail;
        private void FrmDrugsIn_Load(object sender, EventArgs e)
        {
            this.dateTimePicker2.Value = this.dateTimePicker2.Value.AddYears(2);
            // TODO: 这行代码将数据加载到表“drugsStoreRoom.StoreRoom”中。您可以根据需要移动或删除它。
            this.storeRoomTableAdapter.Fill(this.drugsStoreRoom.StoreRoom);

            //窗体最大化
            this.WindowState = FormWindowState.Maximized;

            //记录当前时间，Enable Timer1控件
            this.timer1.Interval = 100;
            this.timer1.Enabled = true;
            this.dateTimePicker1.Text = DateTime.Now.ToString();

            //获取用户工号
            this.txtBoxKPYID.Text = u_no;
            try
            {
                DataTable dt = new DataTable();
                string sql = "SELECT [drugsBuyId] ID,[drugsBuyCode] 采购单号,[drugsId] 药品ID,[drugsName] 药品名称,[drugsPY] 拼音码,[drugsSpecification] 药品规格,[drugsUnit]计量单位 ,[drugsRegNum]批准文号,[drugsBuyNum]采购数量 ,[drugsBuyPrice]采购单价 ,[drugsSalePrice]销售价格,[drugsBuyFactory]生产厂家,[drugsBuyProvider]供应商 ,[drugsBuyStoreRoom]库房 ,[drugsBuyBatch]批号,[drugsBuyTotalPrice]总价 ,[drugsBuyerName]采购人员 ,[drugsBuyDate]采购日期 FROM [Drugs].[dbo].[drugsBuy] where drugsBuyInFlag is null and drugsBuyOutFlag is null and drugsBuyDel is null ORDER BY ID";
                dt = op.getDataTable(sql, "drugsBuy");
                dataGridView1.DataSource = dt;
                this.txtBoxDrugsInMan.Text = u_name;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                DataTable dt_Search = new DataTable();
                dt_Search = op.getDataTable("SELECT [drugsBuyId] ID,[drugsBuyCode] 采购单号,[drugsId] 药品ID,[drugsName] 药品名称,[drugsPY] 拼音码,[drugsSpecification] 药品规格,[drugsUnit]计量单位 ,[drugsRegNum]批准文号,[drugsBuyNum]采购数量 ,[drugsBuyPrice]采购单价 ,[drugsSalePrice]销售价格,[drugsBuyFactory]生产厂家,[drugsBuyProvider]供应商 ,[drugsBuyStoreRoom]库房 ,[drugsBuyBatch]批号,[drugsBuyTotalPrice]总价 ,[drugsBuyerName]采购人员 ,[drugsBuyDate]采购日期 FROM [Drugs].[dbo].[drugsBuy]  where drugsBuyId='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "' ORDER BY ID", "drugsBuy");
                this.txtBoxDrugsBuyId.Text = dt_Search.Rows[0]["ID"].ToString();
                this.txtBoxDrugsName.Text = dt_Search.Rows[0]["药品名称"].ToString();
                this.txtBoxDrugsPY.Text = dt_Search.Rows[0]["拼音码"].ToString();
                this.txtBoxdrugsSpecification.Text = dt_Search.Rows[0]["药品规格"].ToString();
                this.txtBoxdrugsUnit.Text = dt_Search.Rows[0]["计量单位"].ToString();
                this.txtBoxRegNum.Text = dt_Search.Rows[0]["批准文号"].ToString();
                this.txtBoxDrugsPrice.Text = dt_Search.Rows[0]["采购单价"].ToString();
                this.txtBoxDrugsSalePrice.Text = dt_Search.Rows[0]["销售价格"].ToString();
                this.txtBoxTotalPrice.Text = dt_Search.Rows[0]["总价"].ToString();
                this.txtBoxDrugsFactory.Text = dt_Search.Rows[0]["生产厂家"].ToString();
                this.txtBoxDrugsInNo.Text = dt_Search.Rows[0]["采购数量"].ToString();
                this.txtBoxdrugsBatchNo.Text = dt_Search.Rows[0]["批号"].ToString();
                this.txtBoxDrugsProvider.Text = dt_Search.Rows[0]["供应商"].ToString();
                this.txtBoxDrugsBuyCode.Text = dt_Search.Rows[0]["采购单号"].ToString();
                this.txtBoxDrugsID.Text = dt_Search.Rows[0]["药品ID"].ToString();//之所以放到后面是因为要获得药品基本信息然后插入到库存表，为之前库存表为空的情况准备
                this.button1.Enabled = true;
                //生成入库单号
                GetDrugsInCode();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Dispose();
            this.Close();
        }

        private void txtBoxSearch_TextChanged(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                string sql = "SELECT [drugsBuyId] ID,[drugsBuyCode] 采购单号,[drugsId] 药品ID,[drugsName] 药品名称,[drugsPY] 拼音码,[drugsSpecification] 药品规格,[drugsUnit]计量单位 ,[drugsRegNum]批准文号,[drugsBuyNum]采购数量 ,[drugsBuyPrice]采购单价 ,[drugsSalePrice]销售价格,[drugsBuyFactory]生产厂家,[drugsBuyProvider]供应商 ,[drugsBuyStoreRoom]库房 ,[drugsBuyBatch]批号,[drugsBuyTotalPrice]总价 ,[drugsBuyerName]采购人员 ,[drugsBuyDate]采购日期 FROM [Drugs].[dbo].[drugsBuy] where drugsName like '%" + this.txtBoxSearch.Text.Trim() + "%'or drugsPY like '%" + this.txtBoxSearch.Text.ToUpper() + "%' order by drugsId";
                dt = op.getDataTable(sql, "drugsBuy");
                dataGridView1.DataSource = dt;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (txtBoxdrugsBatchNo.Text.Trim() == "")
            {
                MessageBox.Show("请输入药品【" + txtBoxDrugsName.Text+ "】的批号！");
                return;
            }
            try
            {
                //使button1不能继续按下，免得误操作
                this.button1.Enabled = false;
                //修改采购表的已入库标志及入库信息  drugsBuyInFlag='1'表示已入库
                DataTable dt1 = new DataTable();
                string sql_1 = "update drugsBuy set drugsBuyInFlag='1',drugsBuyInID=' " + u_no + "',drugsBuyInMan='" + u_name + "',drugsBuyInDate='" + this.dateTimePicker1.Value + "'where drugsBuyId='" + this.txtBoxDrugsBuyId.Text.Trim() + "'";
                dt1 = op.getDataTable(sql_1, "drugsStore");

                //插入到入库表
                string sql_a = String.Format("insert into drugsIn(drugsBuyCode,drugsId,drugsInCode,drugsUsefulDate,drugsInStorePlace,drugsInManID,drugsInMan,drugsInDate,drugsInNum) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}')",txtBoxDrugsBuyCode.Text,txtBoxDrugsID.Text,txtBoxDrugsInNum.Text,dateTimePicker2.Text,cbStorePlace.Text,txtBoxKPYID.Text,txtBoxDrugsInMan.Text,dateTimePicker1.Value,this.txtBoxDrugsInNo.Text);
                op.ExecuteSql(sql_a);
                MessageBox.Show("药品【" + txtBoxDrugsName.Text.ToString() + "】入库成功！\r\n 该药品采购单号为【" + this.txtBoxDrugsBuyCode.Text.ToString() + "】\r\n该药品入库单号为【" + txtBoxDrugsInNum.Text + "】", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                //判断是否有库存,如果有则获取库存量，否则赋值为零库存
                DataTable dt2 = new DataTable();
                dt2 = op.getDataTable("select drugscount from drugsstore where drugsid ='" + this.txtBoxDrugsID.Text.ToString() + "'", "drugsstore");
                if (dt2.Rows.Count > 0)
                {     //如果库存表有记录则操作本段,功能：获得drugsId号对应的库存量
                    string drugsid = txtBoxDrugsID.Text.Trim();
                    string sql = "select drugscount from drugsstore where drugsid='" + drugsid + "'";
                    drugsCount = op.getDataTable(sql).Rows[0][0].ToString();
                }
                else
                {
                    drugsCount = "0";
                }

                //修改库存表
                int drugsNewCount = Convert.ToInt32(drugsCount) + Convert.ToInt32(this.txtBoxDrugsInNo.Text.ToString());
                drugsCountAfterDrugsIn = drugsNewCount.ToString();
                DataTable dt3 = new DataTable();
                string sql_3 = "update drugsStore set drugsCount=' " + drugsCountAfterDrugsIn + "' where drugsID='" + this.txtBoxDrugsID.Text.Trim() + "'";
                dt3 = op.getDataTable(sql_3, "drugsStore");

                //刷新datagridview的数据
                DataTable dt = new DataTable();
                string sql_datagriview = "SELECT [drugsBuyId] ID,[drugsBuyCode] 采购单号,[drugsId] 药品ID,[drugsName] 药品名称,[drugsPY] 拼音码,[drugsSpecification] 药品规格,[drugsUnit]计量单位 ,[drugsRegNum]批准文号,[drugsBuyNum]采购数量 ,[drugsBuyPrice]采购单价 ,[drugsSalePrice]销售价格,[drugsBuyFactory]生产厂家,[drugsBuyProvider]供应商 ,[drugsBuyStoreRoom]库房 ,[drugsBuyBatch]批号,[drugsBuyTotalPrice]总价 ,[drugsBuyerName]采购人员 ,[drugsBuyDate]采购日期 FROM [Drugs].[dbo].[drugsBuy] where drugsBuyInFlag is null and drugsBuyOutFlag is null and drugsBuyDel is null ORDER BY ID";
                dt = op.getDataTable(sql_datagriview, "drugsBuy");
                dataGridView1.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 生成入库单号
        /// </summary>
        private void GetDrugsInCode()
        {
            try
            {
                ////该语句获得201268格式的日期，弃用
                //string str = Convert.ToString(DateTime.Now.Date.Year) + Convert.ToString(DateTime.Now.Date.Month) + Convert.ToString(DateTime.Now.Date.Day);

                //设置dateTimePicker3可见，以便获得如20120608格式的日期
                this.dateTimePicker3.Visible = true;

                this.txtBoxDrugsInNum.Text = this.txtBoxKPYID.Text + this.dateTimePicker3.Text + txtBoxDrugsBuyCode.Text;

                //设置dateTimePicker3不可见
                this.dateTimePicker3.Visible = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private void btnDrugsInDetail_Click(object sender, EventArgs e)
        {
            frmDrugsInDetail = new FrmDrugsInDetail();
            frmDrugsInDetail.ShowDialog();
        }

        /// <summary>
        /// 如果库存表无该药品库存记录则置库存为零，插入一条新数据到库存表 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtBoxDrugsID_TextChanged(object sender, EventArgs e)
        {
            try
            {
                //如果txtBoxDrugsID的值不为空的时候执行
                if (this.txtBoxDrugsID.Text.Trim()!="")
                {
                    DataTable dt2 = new DataTable();
                    dt2 = op.getDataTable("select drugscount from drugsstore where drugsid ='" + this.txtBoxDrugsID.Text.ToString() + "'", "drugsstore");
                    if (dt2.Rows.Count == 0)
                    {   //如果库存表无该药品库存记录则置库存为零，插入一条新数据到库存表
                        string sql_a = String.Format("insert into drugsstore(drugsId,drugsName,drugsPY,drugsSpecification,drugsUnit,drugsRegNum,drugsFactory,drugsCount) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')", txtBoxDrugsID.Text, txtBoxDrugsName.Text, txtBoxDrugsPY.Text, txtBoxdrugsSpecification.Text, txtBoxdrugsUnit.Text, txtBoxRegNum.Text, txtBoxDrugsFactory.Text, "0");
                        op.ExecuteSql(sql_a);
                    }
                }
                
            }
            catch (Exception ex )
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

        }
    }
}
